package com.baichang.taoshunda.listener;

import com.baichang.framework.utils.LogUtils;
import com.baichang.taoshunda.ServiceConstant;
import com.baichang.taoshunda.service.InitProjectsService;
import com.baichang.taoshunda.service.ServerService;
import com.baichang.taoshunda.service.TaoshundaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;


/**
 * Created by yuliang on 2016/9/7.
 */
public class ServerListener implements ServletContextListener {


    private ServerService serverService;

    private WebApplicationContext springContext;
    private InitProjectsService initProjectsService;

    @Override
    public void contextDestroyed(ServletContextEvent e) {
        initProjectsService.close();

    }

    @Override
    public void contextInitialized(ServletContextEvent event) {
        springContext = WebApplicationContextUtils
                .getWebApplicationContext(event.getServletContext());
        initProjectsService = springContext.getBean(InitProjectsService.class);
        serverService = springContext.getBean(ServerService.class);
        initProjectsService.start();
        serverService.start();
        LogUtils.getInstance().info("ServerListener contextInitialized initProjectsService is: " + initProjectsService);
        LogUtils.getInstance().info("ServerListener contextInitialized initProjectsService.start(); ");
        //注入任务调度Service
        ServiceConstant.taoshundaService = springContext.getBean(TaoshundaService.class);
        LogUtils.getInstance().info("ServerListener contextInitialized jiLiwaimaiService is: " + ServiceConstant.taoshundaService);

    }
}
